<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>管理数据源</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
    <link rel="stylesheet" type="text/css" href="public/css/ext-all.css"/>
    <link rel="stylesheet" type="text/css" href="public/css/ext-zh_CN.css"/>
    <script type="text/javascript" src="public/js/ext-all.js"></script>
    <script type="text/javascript" src="public/js/ext-lang-zh_CN.js"></script>
    <script type="text/javascript" src="public/js/base.js"></script>
    <script type="text/javascript" src="public/js/store/dataBaseStore.js"></script>
</head>
<body>
<script>
    Ext.onReady(function () {
        Ext.getBody().mask('加载中...');

        var buttonPanel = Ext.create('Ext.Panel', {
            id: 'buttonPanel',
            defaults: {
                style: 'margin: 2px;'
            },
            items: [{
                xtype: 'button',
                text: '查询',
                icon: 'public/image/btn/search.png',
                handler: _selectDataBaseInfo
            }, {
                xtype: 'button',
                text: '新增',
                icon: 'public/image/btn/add.png',
                handler: _preInsertDataBaseInfo
            }, {
                xtype: 'button',
                text: '修改',
                icon: 'public/image/btn/edit.png',
                handler: _preUpdateDataBaseInfo
            }, {
                xtype: 'button',
                text: '删除',
                icon: 'public/image/btn/delete.png',
                handler: _deleteDataBaseInfo
            }, {
                xtype: 'button',
                text: '查看',
                icon: 'public/image/btn/view.png',
                handler: _viewDataBaseInfo
            }, {
                xtype: 'button',
                text: '生成数据库文档',
                icon: 'public/image/btn/association.png',
                handler: _preGenDataBaseDocument
            }]
        });

        var dataBasePanel = Ext.create('Ext.grid.Panel', {
            id: 'dataBasePanel',
            store: dataBaseStore,
            columnLines: true,
            selModel: {
                selType: 'checkboxmodel',
                mode: 'SINGLE'
            },
            frame: true,
            columns: [{
                xtype: 'rownumberer',
                align: 'center',
                width: 50
            }, {
                text: '数据源名称',
                dataIndex: 'NAME',
                flex: 1,
                minWidth: 150
            }, {
                text: '驱动类名称',
                dataIndex: 'DRIVER',
                flex: 2,
                minWidth: 200
            }, {
                text: '链接',
                dataIndex: 'URL',
                flex: 2,
                minWidth: 200
            }, {
                text: '用户名',
                dataIndex: 'USERNAME',
                width: 150
            }, {
                text: '创建日期',
                dataIndex: 'CREATE_TIME',
                width: 150
            }, {
                text: '更新日期',
                dataIndex: 'UPDATE_TIME',
                width: 150
            }],
            viewConfig: {
                emptyText: '<div style="text-align: center; padding-top: 50px; font: italic bold 20px Microsoft YaHei;">没有数据</div>',
                enableTextSelection: true
            },
            listeners: {
                'itemdblclick': function (dataview, record, item, index, e) {
                    _viewDataBaseInfo();
                }
            }
        });

        var viewDataBasePanel = Ext.create('Ext.form.Panel', {//表单
            id: 'viewDataBasePanel',
            layout: 'column',
            frame: true,
            autoScroll: true,
            defaults: {
                labelAlign: 'right',
                labelWidth: 150,
                inputWidth: 300,
                margin: '4'
            },
            items: [{
                xtype: 'textfield',
                name: 'NAME',
                fieldLabel: '数据源名称',
                style: 'clear:both',
                readOnly: true
            }, {
                xtype: 'textfield',
                name: 'DRIVER',
                fieldLabel: '驱动类名称',
                style: 'clear:both',
                readOnly: true
            }, {
                xtype: 'textfield',
                name: 'URL',
                inputWidth: 600,
                fieldLabel: '链接',
                style: 'clear:both',
                readOnly: true
            }, {
                xtype: 'textfield',
                name: 'USERNAME',
                fieldLabel: '用户名',
                style: 'clear:both',
                readOnly: true
            }, {
                xtype: 'textfield',
                name: 'PASSWORD',
                fieldLabel: '密码',
                style: 'clear:both',
                readOnly: true
            }, {
                xtype: 'textfield',
                name: 'CREATE_TIME',
                fieldLabel: '创建日期',
                style: 'clear:both',
                readOnly: true
            }, {
                xtype: 'textfield',
                name: 'UPDATE_TIME',
                fieldLabel: '更新日期',
                style: 'clear:both',
                readOnly: true
            }]
        });

        Ext.create('Ext.container.Viewport', {
            layout: {
                type: 'border',
                regionWeights: {
                    west: 1,
                    north: -1,
                    south: -1,
                    east: 1
                }
            },
            defaults: {
                border: false
            },
            items: [{
                region: 'north',
                items: [buttonPanel]
            }, {
                region: 'center',
                layout: 'fit',
                items: [dataBasePanel]
            }]
        });

        _init();
    });

    function _init() {
        for (var i = 0; i < Ext.data.StoreManager.getCount(); i++) {
            if (Ext.data.StoreManager.getAt(i).isLoading()) {
                return;
            }
        }

        _selectDataBaseInfo();
        Ext.getBody().unmask();
    }

    function _viewDataBaseInfo() {
        var records = Ext.getCmp('dataBasePanel').getSelectionModel().getSelection();
        if (records.length === 0) {
            Ext.MessageBox.alert('警告', '请选择数据源', Ext.MessageBox.WARNING);
            return;
        }
        var viewDataBaseForm = Ext.getCmp('viewDataBasePanel').getForm();
        for (var key in records[0].data) {
            (viewDataBaseForm.findField(key) != null) ? viewDataBaseForm.findField(key).setValue(records[0].data[key]) : 0;
        }
        var viewDataBasePanel = Ext.getCmp('viewDataBasePanel');
        var win = Ext.create('Ext.window.Window', {
            title: '详情信息',
            modal: true,
            autoShow: true,
            maximized: false,
            layout: 'fit',
            width: document.documentElement.clientWidth * 0.6,
            height: document.documentElement.clientHeight * 0.8,
            closeAction: 'hide', //关闭窗体实际上是隐藏窗体并未关闭销毁此窗体对象(节约资源)
            items: [viewDataBasePanel]
        });
        win.show();
    }

    function _preInsertDataBaseInfo() {
        returnValue = null;
        win = Ext.create('Ext.window.Window', {
            title: '新增数据源',
            modal: true,
            autoShow: true,
            maximizable: true,
            width: document.documentElement.clientWidth * 0.6,
            height: document.documentElement.clientHeight * 0.8,
            html: '<iframe src="/gen/preInsertDataBaseInfo" style="width: 100%; height: 100%;" frameborder="0"></iframe>',
            listeners: {
                close: function (panel, eOpts) {
                    if (returnValue !== null) {
                        if (returnValue === true) {
                            _selectDataBaseInfo();
                            Toast.alert('信息', '新增成功', 2000);
                        } else {
                            Ext.MessageBox.alert('错误', '新增失败', Ext.MessageBox.ERROR);
                        }
                    }
                }
            }
        });
    }

    function _preUpdateDataBaseInfo() {
        var records = Ext.getCmp('dataBasePanel').getSelectionModel().getSelection();
        if (records.length === 0) {
            Ext.MessageBox.alert('警告', '请选择一条数据', Ext.MessageBox.WARNING);
            return;
        }

        returnValue = null;
        win = Ext.create('Ext.window.Window', {
            title: '修改数据源',
            modal: true,
            autoShow: true,
            maximized: false,
            maximizable: true,
            width: document.documentElement.clientWidth * 0.6,
            height: document.documentElement.clientHeight * 0.8,
            html: '<iframe src="/gen/preUpdateDataBaseInfo?ID=' + records[0].get('ID') + '" style="width: 100%; height: 100%;" frameborder="0"></iframe>',
            listeners: {
                close: function (panel, eOpts) {
                    if (returnValue != null) {//更新页面数据
                        if (returnValue === true) {
                            _selectDataBaseInfo();
                            Toast.alert('信息', '修改成功', 2000);
                        } else {
                            Ext.MessageBox.alert('错误', '修改失败', Ext.MessageBox.ERROR);
                        }
                    }
                }
            }
        });
    }

    function _deleteDataBaseInfo() {
        var records = Ext.getCmp('dataBasePanel').getSelectionModel().getSelection();
        if (records.length === 0) {
            Ext.MessageBox.alert('警告', '请选择数据源', Ext.MessageBox.WARNING);
            return;
        }

        Ext.MessageBox.show({
            title: '请确认',
            msg: '是否删除?',
            buttons: Ext.MessageBox.YESNO,
            icon: Ext.MessageBox.QUESTION,
            fn: function (btn) {
                if (btn === 'yes') {
                    Ext.Ajax.request({
                        url: '/gen/deleteDataBaseInfo',
                        async: false,
                        params: {
                            'ID': records[0].get('ID'),
                            'URL': records[0].get('URL'),
                            'USERNAME': records[0].get('USERNAME')
                        },
                        callback: function (options, success, response) {
                            if (success) {
                                var data = Ext.decode(response.responseText);
                                if (data.success === true) {
                                    _selectDataBaseInfo();
                                    Toast.alert('信息', '删除成功', 2000);
                                } else {
                                    Ext.MessageBox.alert('错误', '删除失败', Ext.MessageBox.ERROR);
                                }
                            } else {
                                Ext.MessageBox.alert('错误', '服务器错误', Ext.MessageBox.ERROR);
                            }
                        }
                    });
                }
            }
        });
    }

    //生成数据库文档
    function _preGenDataBaseDocument() {
        var records = Ext.getCmp('dataBasePanel').getSelectionModel().getSelection();
        if (records.length === 0) {
            Ext.MessageBox.alert('警告', '请选择数据源', Ext.MessageBox.WARNING);
            return;
        }
        //测试连接
        Ext.Ajax.request({
            url: '/gen/testConnect',
            async: false,
            params: {
                'URL': records[0].get('URL'),
                'DRIVER': records[0].get('DRIVER'),
                'USERNAME': records[0].get('USERNAME'),
                'PASSWORD': records[0].get('PASSWORD')
            },
            callback: function (options, success, response) {
                if (success) {
                    var data = Ext.decode(response.responseText);
                    if (data.success === true) {
                        //跳转数据库文档生成页
                        var url = '?URL=' + encodeURIComponent(records[0].get('URL')) +
                            '&DRIVER=' + encodeURIComponent(records[0].get('DRIVER')) +
                            '&USERNAME=' + encodeURIComponent(records[0].get('USERNAME')) +
                            '&PASSWORD=' + encodeURIComponent(records[0].get('PASSWORD')) +
                            '&NAME=' + encodeURIComponent(records[0].get('NAME'));
                        win = Ext.create('Ext.window.Window', {
                            title: '数据库文档生成',
                            modal: true,
                            autoShow: true,
                            maximized: false,
                            maximizable: true,
                            width: document.documentElement.clientWidth * 0.6,
                            height: document.documentElement.clientHeight * 0.8,
                            html: '<iframe src="/gen/preGenDataBaseDocument' + url + '" style="width: 100%; height: 100%;" frameborder="0"></iframe>'
                        });
                    } else {
                        Ext.MessageBox.alert('错误', data.message, Ext.MessageBox.ERROR);
                    }
                } else {
                    Ext.MessageBox.alert('错误', '服务器错误', Ext.MessageBox.ERROR);
                }
            }
        });
    }
</script>
</body>
</html>